<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/28
 * Time: 11:35
 */

namespace app\admin\controller;


use app\common\model\Bankroll;
use app\common\model\CapitalFlow;
use app\common\model\UserAccount;
use think\Db;
use think\Exception;

class Money extends Base
{

    protected $status = [
        1 => ['status' => '0' ,'msg' => '全部'],
        2 => ['status' => '2' ,'msg' => '待审核'],
//        3 => ['status' => '3' ,'msg' => '提现处理中'],
        4 => ['status' => '4' ,'msg' => '驳回'],
        5 => ['status' => '5' ,'msg' => '审核通过'],
    ];


    /**
     * 流水类型
     */
    protected $stream_status = [
        1 => ['key' => '1' ,'val' => '平台流水'],
        2 => ['key' => '2' ,'val' => '用户充值'],
        3 => ['key' => '3' ,'val' => '用户提现'],
    ];




    /**
     * 充值记录列表
     */
    public function index()
    {
        $where = [];
        if (!empty($_GET['status'])) $where['a.status']  = input('get.status');
        if (!empty($_GET['nick_name'])) $where['u.nick_name']  = ['like','%'.trim(input('get.nick_name')).'%'];
        $where['a.type'] = 1;
        $model = new Bankroll();
        $rows  = $model->cashList($where);
        $this->assign([
            'title' => '充值记录',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }

    /**
     * 充值详情
     */
    public function detail2()
    {
        $model = new Bankroll();
        $where['a.b_id'] = input('id');
        $data = $model->getDetail($where);
        $this->assign([
            'data' => $data,
            'title' => '充值',
            'status' => $this->status,
        ]);
        return view();
    }


    /**
     * 提现申请列表
     */
    public function cash()
    {
        $where = [];
        if (is_numeric($_GET['status'])){
            if ($_GET['status'] != 0){
                $where['a.status']  = input('get.status');
            }
        }else{
            $where['a.status']  = 2;
        }

        if (!empty($_GET['nick_name'])) $where['u.nick_name'] = ['like','%'.trim(input('get.nick_name')).'%'];
        $where['a.type'] = 2;
        $model = new Bankroll();
        $rows  = $model->cashList($where);
        $this->assign([
            'getStatus' => $where['a.status'],
            'title' => '提现申请列表',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
            'status' => $this->status
        ]);
        return view();
    }

    /**
     * 提现审核
     */
    public function edit()
    {
        $model = new Bankroll();
        if (request()->isPost()){
            $data = input('post.');
            $repeat = cache('cash_edit_repeat_'.$data['id']);
            if ($repeat){ //防止重复操作
                $this->error('请求过于频繁,请稍后重试');
            }else{
                cache('cash_edit_repeat_'.$data['id'],1,3);
            }
            $cash = $model->getDetail(['b_id'=>$data['id']]);

            if ($cash['status'] !== 2) $this->error('该申请已处理,请勿重复操作');
            $update['update_time'] = time();
            if ($data['status'] == 1){//审核通过

                switch ($cash['acType']){ //银行卡
                    case '1':

                        $appkey  = config('yun')['appkey'];
                        $des3key = config('yun')['des3key'];
                        $order = $data['id'];

                        $real_name = $cash['real_name'];
                        $card_no   = $cash['account'];
                        $id_card   = $cash['ID_num'];
                        $pay       = $cash['money'];

                        //银行卡data
                        $string = <<<EOF
{"order_id": "$order","dealer_id": "07821809","broker_id":
"yiyun73","real_name": "$real_name","card_no": "$card_no","id_card": "$id_card","pay": "$pay"}
EOF;

                        $url = 'https://api-jiesuan.yunzhanghu.com';

                        $time = time();

                        $mess = rand(111111,999999);

                        $result = encrypt($string,$des3key);

                        $sign = hash_hmac('sha256',"data=$result&mess=$mess&timestamp=$time&key=$appkey",$appkey);

                        $array['data'] = $result;
                        $array['mess'] = $mess;
                        $array['timestamp'] = $time;
                        $array['sign']  = $sign;
                        $array['sign_type'] = 'sha256';

                        $res = makeOrder($url.'/api/payment/v1/order-realtime',$array);

                        if ($res['code'] === 0000 || $res['code'] === '0000'){
                            //提交订单成功
//                            sleep(3);

//                            dd($this->orderStatus($cash['b_id']));


                        }else{
                            $this->error($res['message']);
                        }


                        $update['status'] = 5;
                        Db::name('bankroll')->where('b_id',$data['id'])->update($update);

                        break;
                    case '2'://支付宝
                        $update['status'] = 5;
                        Db::name('bankroll')->where('b_id',$data['id'])->update($update);
                        //TODO 支付宝申请下来后开放自动打款
                        break;
                    default:
                        api_return(0,'未处理提现方式');
                }

            }else{//驳回
                Db::startTrans();
                try{
                    $update['status'] = 4;
                    Db::name('bankroll')->where('b_id',$data['id'])->update($update);
                    Db::name('users')->where('user_id',$cash['user_id'])->setInc('cash',$cash['money']);
                    Db::name('users')->where('user_id',$cash['user_id'])->setInc('cash',$cash['ratio_money']);
                    Db::commit();
                }catch (Exception $e){
                    Db::rollback();
                    $this->error('系统错误');
                }
            }
            if ($data['status'] == 1){
                $str = sprintf('您的提现申请%s元已成功受理，预计1-2个工作日到账，请留意到账信息。如有疑问请及时与我们联系。',$cash['money']);
//                $phone = \app\api\controller\Base::staticInfo('phone',$cash['user_id']);
//
//                $val['val1'] = substr($phone,-4);
//                $val['val2'] = $cash['money'];
//                sendCustomSms($phone,$val,'cash');
            }else{
                $msg = '';
                if ($data['remark']){
                    $msg = '拒绝原因为:'.$data['remark'];
                }
                $str = sprintf('您的提现申请%s元未审核通过。',$cash['money']);
                $str.=$msg;
            }

            \app\api\controller\Base::sendUsrMsg('system',hashid($cash['user_id']),$str);
            $this->success('审核成功',url('cash'));

        }
        $where['a.b_id'] = input('id');
        $data = $model->getDetail($where);
        $this->assign([
            'title' => '提现审核',
            'data' => $data,
            'status' => $this->status,
        ]);
        return view();
    }





    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 统计图表
     */
    public function map(){
        $map=[];
        if(!empty($_GET['status'])){
            $map['status']=intval(trim(input('get.status')));
        }else{
            $map['status'] = ['in','1,5'];
        }


        if(!empty($_GET['startDate'])){
            $startDate=strtotime(trim(input('get.startDate')));
        }
        if(!empty($_GET['endDate'])){
            $endDate=strtotime(trim(input('get.endDate')));
        }
        if($startDate&&$endDate){
            $map['update_time']=['between time',[$startDate,$endDate]];
        }else if($startDate&&empty($endDate)){
            $map['update_time']=['> time',$startDate];
        }else if($endDate&&empty($startDate)){
            $map['update_time']=['< time',$endDate];
        }
        $res  = Db::name('bankroll')->where($map)->field('create_time,money,status,type')->order('create_time')->select();
        $rows = [];
        foreach ($res as $k => $v){
            $date = date('Y-m-d',$v['create_time']);
            if ($v['status'] == 1){ //充值
                if(array_key_exists($date,$rows)){
                    $rows[$date]['recharge'] += $v['money'];
                }else{
                    $rows[$date]['recharge']  = $v['money'];
                    $rows[$date]['cash'] = 0;
                }
            }else{ //提现
                if(array_key_exists($date,$rows)){
                    $rows[$date]['cash'] += $v['money'];
                }else{
                    $rows[$date]['cash']  = $v['money'];
                    $rows[$date]['recharge'] = 0;
                }
            }
        }
        $recharge = json_encode(array_column($rows,'recharge'));
        $cash = json_encode(array_column($rows,'cash'));
        $days  = json_encode(array_keys($rows));
        $this->assign([
            'days'=>$days,
            'recharge'=>$recharge,
            'cash'=>$cash,
            'status'=>input('get.status')
        ]);
        return view();
    }

    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 平台资金流水
     */
    public function stream()
    {
        $where = [];

        if (!isEmpty(input('status'))){
            $where['a.status'] = input('status');
        }

        if (!isEmpty(input('money_type'))){
            $where['a.money_type'] = input('money_type');
        }


        if (!empty(input('startDate'))){
            if (empty(input('endDate'))){
                $where['a.create_time'] = ['> time',input('startDate')];
            }else{
                $where['a.create_time'] = ['between time',[input('startDate'),input('endDate')]];
            }
        }elseif (!empty(input('endDate'))){
            $where['a.create_time'] = ['<= time',input('endDate')];
        }
//        dump($where);exit;


        $model = new CapitalFlow();
        $rows  = $model->getList($where);

        $where['a.status'] = 1;
        //平台流水统计
        $stream = $model->alias('a')->where($where)->sum('money');

        //充值统计
        $where['a.status'] = 2;
        $recharge = $model->alias('a')->where($where)->sum('money');

        //提现统计
        $where['a.status'] = 3;
        $cash = $model->alias('a')->where($where)->sum('money');


        $coin = Db::name('coin')->select();
        $money_type = array_key($coin,'coin_id');
        $this->assign([
            'title' => '平台流水',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
            'money_type' => $money_type,
            'status' => $this->stream_status,
            'stream' => $stream,
            'recharge' => $recharge,
            'cash' => $cash,
        ]);
        return view();
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 提现账号
     */
    public function account()
    {

        $where = [];
        if (!empty($_GET['status'])) $where['a.status']  = input('get.status');
        if (!empty($_GET['nick_name'])) $where['u.nick_name']  = ['like','%'.trim(input('get.nick_name')).'%'];

        $model = new UserAccount();
        $rows  = $model->getList($where);
        $this->assign([
            'title' => '充值记录',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }

    public function del()
    {
        $this->_del('user_account');
    }


    public function change()
    {
        $data = input();
        $result = Db::name('user_account')->update(['status'=>$data['type'],'account_id'=>$data['id']]);

        if($result !== false){

            if ($data['type'] == 1){ //通过审核
                $str = '您的提现账号已通过审核';
            }else{
                $str = '您的提现账号未通过审核';
            }

            $account =  Db::name('user_account')->find($data['id']);

            if ($data['msg']){
                $str.=':'.$data['msg'];
            }

            \app\api\controller\Base::sendUsrMsg('system',hashid($account['user_id']),$str);

            $this->success('操作成功');
        }
        $this->error('操作失败');
    }





    
}